﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.RAL.Access;
using Empires.CRL.DataContract;
using Empires.RAL.ORM;
using Empires.RAL.ORM.CompanyData;

namespace Empires.RAL.Pump
{
    public class CompanySettingDataPump : ICompanySettingAccess
    {
        public string GetTitleAndKeyWord(string copName)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_complanysetting
                              where c.c_vc_cpname == copName
                              select new { c.c_nvc_title, c.c_nvc_keyword }).FirstOrDefault();
                return result == null ? "$" : String.Format("{0}${1}", result.c_nvc_title, result.c_nvc_keyword);
            }
        }

        public List<DefaultDataPack> GetLogoInfo(string copName)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_complanysetting
                              where c.c_vc_cpname == copName
                              select new { c.c_nvc_logoImg,c.c_nvc_title }).FirstOrDefault();
                if (result != null)
                {
                    return new List<DefaultDataPack>() { 
                        new DefaultDataPack(){ 
                            URL=result.c_nvc_logoImg,
                            TITLE=result.c_nvc_title}
                    };
                }
                return null;
            }
        }

        public List<DefaultDataPack> GetScrollImgs(string copname, int take)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_maininfo
                              where c.c_vc_cpname == copname && !c.c_b_isdelete
                              orderby c.c_i_id descending
                              select new
                              {
                                  c.c_vc_photo,
                                  c.c_vc_title,
                                  c.c_vc_link,
                                  c.c_i_type,
                                  c.c_i_subject
                              }).Take(take).ToList();
                List<DefaultDataPack> list = new List<DefaultDataPack>();
                if (result != null)
                {
                    list = result.Select(c => new DefaultDataPack()
                    {
                        URL = c.c_vc_photo,
                        TITLE = c.c_vc_title,
                        INFOLINK = c.c_vc_link,
                        TYPE = c.c_i_type,
                        SUBJECT = c.c_i_subject
                    }).ToList();
                }
                return list;
                //var result = (from c in ctx.tb_complanysetting
                //              where c.c_vc_cpname == copname
                //              select new { c.c_nvc_scrollImgs }).FirstOrDefault();
                //if (result != null)
                //{
                //    return new List<DefaultDataPack>() { 
                //        new DefaultDataPack(){ 
                //            CONTENT = result.c_nvc_scrollImgs
                //        }
                //    };
                //}
                //return null;
            }
        }
    }
}
